package com.frejoys.store.controller;

import cn.hutool.core.bean.BeanUtil;
import com.frejoys.common.aop.Authority;
import com.frejoys.common.aop.Authorization;
import com.frejoys.common.constant.CommonConstant;
import com.frejoys.common.dto.app.CategoryPidDto;
import com.frejoys.common.dto.app.CategorySiteDto;
import com.frejoys.common.dto.common.IdDto;
import com.frejoys.common.dto.store.CategoryListDto;
import com.frejoys.common.response.Result;
import com.frejoys.common.vo.admin.CategorySelectVo;
import com.frejoys.common.vo.app.CategoryListVo;
import com.frejoys.common.vo.common.SelectVo;
import com.frejoys.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@Tag(name = "品类", description = "品类")
@RestController
@RequiredArgsConstructor
@RequestMapping(CommonConstant.routePrefixStore)
@Authorization(Authority.Private)
public class MallCategoryController {

    private final CategoryService categoryService;


    /**
     * 添加店铺时获取大类
     */
    @Operation(summary = "首页菜单品类列表 大类")
    @PostMapping("/category/listInfo")
    public Result<List<SelectVo>> listInfo() {
        // 复用app端接口
        CategorySiteDto dto = new CategorySiteDto();
        dto.setSite(1);
        List<CategoryListVo> categoryListVos = categoryService.onCategory(dto, categoryService.listInfo());
        List<SelectVo> collect = categoryListVos.stream().map(c ->
                new SelectVo(c.getId(), c.getName())).collect(Collectors.toList());
        return Result.success(collect);
    }

    /**
     * 添加商品时根据店铺的品类获取子品类
     * @param idDto 大类id
     * @return 子类
     */
    @Operation(summary = "根据品类id查询子集品类 包含店铺本身品类")
    @PostMapping("/category/sonListInfo")
    public Result<List<SelectVo>> sonListInfo(@RequestBody @Validated IdDto idDto) {
        // 复用app端接口
        CategoryPidDto dto = new CategoryPidDto();
        dto.setSite(2);
        dto.setId(idDto.getId());
        List<CategoryListVo> categoryListVos = categoryService.categoryListInfo(dto, categoryService.listInfo());
        List<SelectVo> collect = categoryListVos.stream().map(c ->
                new SelectVo(c.getId(), c.getName())).collect(Collectors.toList());
        return Result.success(collect);
    }


    @Operation(summary = "品类列表")
    @PostMapping("/category/list")
    public Result<List<CategorySelectVo>> storeList(@RequestBody @Validated CategoryListDto dto) {
        return Result.success(categoryService.getSelectVo_old(dto));
    }
    @Operation(summary = "品类列表-无嵌套(新)")
    @PostMapping("/category/list2")
    public Result<List<CategorySelectVo>> storeList2(@RequestBody @Validated CategoryListDto dto) {
        return Result.success(categoryService.getSelectVo(categoryService.listAll(),dto));
    }

    @Operation(summary = "全部分类-（新）")
    @PostMapping("/category/categoryAll")
    public Result<List<CategoryListVo>> categoryAll(){
        return Result.success(categoryService.categoryAll(categoryService.listInfo()));
    }

    /**
     * 根据店铺id获取店铺新增商品时能够选择的品类
     * @param dto 店铺id
     */
    @Operation(summary = "根据店铺id获取店铺新增商品时能够选择的品类")
    @PostMapping("/category/listChildCategory")
    Result<List<CategoryListVo>> listChildCategory(@RequestBody @Validated IdDto dto){
        return Result.success(categoryService.listChildCategory(dto));
    }


}
